package com.ruoyi.match.service.impl;

import java.util.List;

import com.ruoyi.match.domain.LeaderTeam;
import com.ruoyi.match.domain.vo.TeamsAddVo;
import com.ruoyi.match.domain.vo.TeamsInfoVo;
import com.ruoyi.match.domain.vo.TeamsVo;
import com.ruoyi.match.mapper.LeaderTeamMapper;
import com.ruoyi.match.mapper.MemberTeamMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.match.mapper.TeamsMapper;
import com.ruoyi.match.domain.Teams;
import com.ruoyi.match.service.ITeamsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 队伍Service业务层处理
 * 
 * @author zhangrui
 * @date 2025-03-26
 */
@Service
public class TeamsServiceImpl implements ITeamsService 
{
    @Autowired
    private TeamsMapper teamsMapper;


    @Autowired
    private LeaderTeamMapper leaderTeamMapper;

    @Autowired
    private MemberTeamMapper memberTeamMapper;

    @Autowired
    private NoticeServiceImpl noticeService;




    /**
     * 查询队伍
     * 
     * @param teamId 队伍主键
     * @return 队伍
     */
    @Override
    public Teams selectTeamsByTeamId(Long teamId)
    {
        return teamsMapper.selectTeamsByTeamId(teamId);
    }

    /**
     * 查询队伍列表
     * 
     * @param teams 队伍
     * @return 队伍
     */
    @Override
    public List<Teams> selectTeamsList(Teams teams)
    {
        return teamsMapper.selectTeamsList(teams);
    }

    /**
     * 新增队伍
     * 
     * @param teams 队伍
     * @return 结果
     */
    @Override
    public int insertTeams(Teams teams)
    {
        return teamsMapper.insertTeams(teams);
    }


    /**
     * 事务新增队伍，队长队伍
     *
     * @param teams 队伍
     * @return 结果
     */
    @Override
    @Transactional
    public int addTeams(TeamsAddVo teams) {

        //新增队伍
        teamsMapper.insertTeams(teams);

        //查询刚刚插入的teamid
        LeaderTeam leaderTeam = new LeaderTeam();
        leaderTeam.setUserId(teams.getLeaderId());
        leaderTeam.setTeamId(teamsMapper.selectLastTeamsId());
        //新增队长队伍
        leaderTeamMapper.insertLeaderTeam(leaderTeam);

        return 1;
    }

    /**
     * 修改队伍
     * 
     * @param teams 队伍
     * @return 结果
     */
    @Override
    public int updateTeams(Teams teams)
    {
        return teamsMapper.updateTeams(teams);
    }

    /**
     * 批量删除队伍
     * 
     * @param teamIds 需要删除的队伍主键
     * @return 结果
     */
    @Override
    public int deleteTeamsByTeamIds(Long[] teamIds)
    {
        return teamsMapper.deleteTeamsByTeamIds(teamIds);
    }

    /**
     * 删除队伍信息
     * 
     * @param teamId 队伍主键
     * @return 结果
     */
    @Override
    public int deleteTeamsByTeamId(Long teamId)
    {
        return teamsMapper.deleteTeamsByTeamId(teamId);
    }

    /**
     * 查询队伍列表
     *
     * @param teams 队伍主键
     * @return 结果
     */
    @Override
    public List<TeamsVo> selectTeamsVoList(Teams teams) {
        return teamsMapper.selectTeamsVoList(teams);
    }


    /**
     * 查询队伍列表
     *
     * @param teamId 队伍主键
     * @return 结果
     */
    @Override
    public TeamsInfoVo selectTeamsInfoVoList(Long teamId) {
        return teamsMapper.selectTeamsInfoVoList(teamId);
    }

    /**
     * 根据活动id查询队伍详情（封装vo）
     * @param actId
     * @return TeamsVo集合
     */
    public List<TeamsVo> selectTeamsVoListByActId(Long actId, Long userId){
        return teamsMapper.selectTeamsVoListByActId(actId, userId);
    }


    /**
     * 根据用户id查询加入的队伍列表（封装vo）
     * @param userId
     * @return TeamsVo集合
     */
    @Override
    public List<TeamsVo> selectJoinedTeamsVoList(Long userId) {

//        先根据userid查询某个用户加入的所有队伍id
        List<Long> teamIds = memberTeamMapper.selectJoinedTeamByUserId(userId);

//        再根据队伍id列表查询所有队伍
        if (teamIds.size() == 0){
            return null;
        }

        return teamsMapper.selectJoinedTeamsVoList(teamIds);
    }


    /**
     * 根据用户id查询领导的队伍列表（封装vo）
     * @param userId
     * @return TeamsVo集合
     */
    @Override
    public List<TeamsVo> showMyTeamList(Long userId) {
//        先根据userid查询某个用户领导的所有队伍id
        List<Long> teamIds = leaderTeamMapper.showMyTeamList(userId);
//        再根据队伍id列表查询所有队伍
        if (teamIds.size() == 0){
            return null;
        }
        return teamsMapper.selectJoinedTeamsVoList(teamIds);
    }



    /**
     * 解散队伍
     * @param teamId
     * @return
     */
    @Transactional
    @Override
    public int deleteTeam(Long teamId) {
        //删除队伍
        teamsMapper.deleteTeamsByTeamId(teamId);
//        删除队长队伍
        leaderTeamMapper.deleteLeaderTeamByTeamId(teamId);
//        删除队员队伍
        memberTeamMapper.deleteMemberTeamByTeamId(teamId);
        //删除队伍的公告
        noticeService.deleteNoticeByTeamId(teamId);



        return 1;
    }

    ;

}
